#include "daysmaster.h"
#include "ui_daysmaster.h"
#include <QDateEdit>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>
#include <QDateTime>

DaysMaster::DaysMaster(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::DaysMaster)
{
    ui->setupUi(this);

    // 初始化日期选择器和标签
    targetDateEdit = ui->dateEdit; // 假设你在Qt Designer中命名为dateEdit
    countdownLabel = ui->label;    // 假设你在Qt Designer中命名为label

    // 连接按钮的点击信号到槽函数
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(onCountdown()));
}

DaysMaster::~DaysMaster()
{
    delete ui;
}


void DaysMaster::onCountdown()
{
    QDateTime targetDateTime = targetDateEdit->dateTime();
    QDateTime currentDateTime = QDateTime::currentDateTime();

    // 计算剩余的天数
    qint64 msecs = targetDateTime.msecsTo(currentDateTime.addDays(1)) / 1000;
    qint64 days = msecs / (24 * 60 * 60);

    if (targetDateTime >= currentDateTime) {
        // 更新标签显示剩余天数
        countdownLabel->setText("距离这一天还有 " + QString::number(-days) + " 天");
    }
    else{
        countdownLabel->setText("这一天已经过去了 " + QString::number(days) + " 天");
    }

}

void DaysMaster::on_pushButton_2_clicked()
{
    QMessageBox judge;
    judge.setStyleSheet("QLabel{""min-width:200px;""min-height:80px;"
                        "font-size:20px;""}");
    judge.setText("    是否清空记录？");
    judge.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
    judge.setDefaultButton(QMessageBox::No);

    int res=judge.exec();
    if(res==QMessageBox::Yes)
    {
        this->close();
        dm = new DaysMaster;
        dm->show();
    }
}


void DaysMaster::on_pushButton_3_clicked()
{
    QMessageBox judge;
    judge.setStyleSheet("QLabel{""min-width:200px;""min-height:80px;"
                        "font-size:20px;""}");
    judge.setText("    是否关闭？");
    judge.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
    judge.setDefaultButton(QMessageBox::No);

    int res=judge.exec();
    if(res==QMessageBox::Yes)
    {
        this->close();
    }
}

